﻿using BusinessLayer;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebFrontEnd
{
    public partial class CreateGuest : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int tryparse;
            if (IsPostBack && int.TryParse(CityInput.Text,out tryparse))
            {
                BusinessEntities.Model.City selectedCity = BL.GetCityByID(tryparse);
                CityInput.Attributes["data-cityid"] = selectedCity.ID.ToString();
                CityInput.Attributes["data-cityname"] = selectedCity.Name;
            }
        }

        protected void GuestTypeRadioInput_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (GuestTypeRadioInput.SelectedValue)
            {
                case "singlemale":
                    MaleFirstNameDiv.ToggleVisibility(true);
                    MaleFirstNameLabel.InnerText = "First Name";
                    MaleFirstNameInput.Attributes["placeholder"] = "Enter your guest's first name";
                    LastNameInput.Attributes["placeholder"] = "Enter your guest's last name";
                    FemaleFirstNameDiv.ToggleVisibility(false);
                    break;
                case "singlefemale":
                    MaleFirstNameDiv.ToggleVisibility(false);
                    FemaleFirstNameLabel.InnerText = "First Name";
                    FemaleFirstNameInput.Attributes["placeholder"] = "Enter your guest's first name";
                    LastNameInput.Attributes["placeholder"] = "Enter your guest's last name";
                    FemaleFirstNameDiv.ToggleVisibility(true);
                    break;
                case "couple":
                    MaleFirstNameDiv.ToggleVisibility(true);
                    MaleFirstNameLabel.InnerText = "Husband's First Name";
                    MaleFirstNameInput.Attributes["placeholder"] = "Enter the husband's first name";
                    FemaleFirstNameDiv.ToggleVisibility(true);
                    FemaleFirstNameLabel.InnerText = "Wife's First Name";
                    FemaleFirstNameInput.Attributes["placeholder"] = "Enter the wife's first name";
                    LastNameInput.Attributes["placeholder"] = "Enter the couple's last name";
                    break;
                default:
                    break;
            }
        }

        [WebMethod]
        public static string GetCities(string q)
        {
            IBL bl = BusinessLayerFactory.GetCurrentBusinessLayer(ConfigurationManager.AppSettings.Get("DomainName"));
            List<object> objList = new List<object>();
            foreach (var city in bl.GetCitiesByKeyWord(q))
            {
                objList.Add(new Dictionary<string, object>()
                {
                    {"id",city.ID},
                    {"name",city.Name}
                });
            }
            var json = new JavaScriptSerializer();
            return json.Serialize(objList);
        }
        //TODO: More data validation.
        protected void CreateButton_Click(object sender, EventArgs e)
        {
            BusinessEntities.Model.Guest guest = new BusinessEntities.Model.Guest();
            guest.User = GetCurrentUser();
            guest.HouseNumber = HouseNumberInput.IntValue();
            guest.ApartmentNumber = ApartmentNumberInput.IntValue();
            switch (GuestTypeRadioInput.SelectedValue)
            {
                case "couple":
                    guest.Couple = true;
                    guest.FemaleFirstName = FemaleFirstNameInput.Text;
                    guest.MaleFirstName = MaleFirstNameInput.Text;
                    break;
                case "singlemale":
                    guest.Couple = false;
                    guest.MaleFirstName = MaleFirstNameInput.Text;
                    guest.FemaleFirstName = null;
                    break;
                case "singlefemale":
                    guest.Couple = false;
                    guest.FemaleFirstName = FemaleFirstNameInput.Text;
                    guest.MaleFirstName = null;
                    break;
                default:
                    break;
            }
            guest.LastName = LastNameInput.Text;
            guest.Street = StreetInput.Text;
            guest.Email = GuestEmail.Text;
            var report = BusinessEntities.ModelValidation.ValidateGuest(guest);
            guest.CityID = CityInput.IntValue();
            if (report.Valid)
            {
                BusinessEntities.Model.Guest retGuest = BL.CreateGuest(guest);
                Response.Redirect("Guests.aspx#gid=" + retGuest.ID.ToString());
            }
        }
    }
}